module Fighter {
	/**
	 *  登陆界面 by 严皓
	*/
	export class StartUI extends eui.Component {
		public static StartUI_sole: StartUI;
		public constructor() {
			super();
			this.skinName = "resource/assets/PlaneE/BeginUI.exml";
			this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
			StartUI.StartUI_sole = this;
			webNetworkMananger.getInstance().connect();

		}
		/**是否登录 */
		private logIn = false;
		/**是否被邀请 */
		public static invited;
		/**登录账户 */
		public static account: string;
		public startBtn: eui.Group;
		public ol_btn: eui.Group;
		/**登录窗口 */
		public loginMain: eui.Group;
		/**登录信息 */
		public loginMsg: eui.Label;
		public is_close: boolean = false;
		public thisFather: any;
		/**是否首次打开 */
		public is_first: boolean = true;
		/**登录倒计时 */
		private loginTime: egret.Timer;
		/**倒计时数字 */
		private loginTime_number: number;
		/**重新登录按钮 */
		private log_again: eui.Group;

		/**新手引导按钮 */
		private helpbtn: eui.Button;
		/**主界面 */
		private mainScene: eui.Group;
		/**空战界面 	airBattleScene的 Group  */
		private airBattleScene: eui.Group;
		/**提示文本 */
		private tipsAirBattle: eui.Label;
		/**准备界面  prepareScene的Group*/
		private prepareScene: eui.Group;
		/**邀请蒙皮  */
		private invitation: eui.Group;

		private onAddToStage(evt: egret.Event) {
			StartUI.invited = false;
			this.mainScene.visible = true;
			this.airBattleScene.visible = false;
			this.prepareScene.visible = false;
			this.invitation.visible = false;
			this.helpbtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.help, this);
			this.startBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.start, this);
			this.ol_btn.touchEnabled = true;
			this.ol_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.oline_start, this);
			this.initialize_login_main();
			messageManage.invitation = -1;
			if (this.is_first) {
				this.is_first=false;
				this.getLaunchInfo();
			}

		}
		/**单机模式 */
		private start(evt: egret.TouchEvent) {
			this.startBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.start, this);
			this.is_close = true;
			GameUtil.PlayerMessage.instance.onePerson = true;
		}
		/**初始化登录窗口 */
		public initialize_login_main() {
			this.loginMain.visible = false;
			this.loginMsg.text = "登录中...";
			this.log_again.visible = false;
		}
		/**联机模式*/
		private oline_start() {
			this.ol_btn.touchEnabled = false;
			console.log("联机模式开启");
			this.ol_btn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.oline_start, this);
			/**没有登录 */
			if (!this.logIn) {
				//创建登录倒计时
				this.loginTime = new egret.Timer(1000, 0);
				this.loginTime.addEventListener(egret.TimerEvent.TIMER, this.login_countdown, this);
				this.loginTime.start();
				this.loginTime_number = 0;
				this.loginMain.visible = true;
			}
			else {
				this.loginMain.visible = false;
			}
			webNetworkMananger.getInstance().LoginGameServer();
			GameUtil.PlayerMessage.instance.onePerson = false;
		}
		/**登录倒计时 */
		private login_countdown() {
			this.loginMsg.text = "登录中..." + (10 - this.loginTime_number).toString() + "s";
			this.loginTime_number++;
			if (this.loginTime_number > 10) {
				this.loginMsg.text = "登录超时，请重新登录";
				this.loginTime.removeEventListener(egret.TimerEvent.TIMER, this.login_countdown, this);
				this.loginTime.stop();
				this.login_again_event();
			}

		}

		/**跳转多人联机模式 */
		public skip_oline_Main(tmpMessage: awesomepackage.s2cLogin) {
			StartUI.account = "success";
			if (tmpMessage.errorCode == 0) {
				this.logIn = true;
				messageManage.userid = tmpMessage.userID;
				console.log(messageManage.userid);
				Scene.SceneManager.SceneManager_sole.addChild(OlUI.OlUI_solo);
				if (StartUI.StartUI_sole.$hasAddToStage) {
					Scene.SceneManager.SceneManager_sole.removeChild(StartUI.StartUI_sole);
				}

			} else {
				this.loginMsg.text = "登录失败\n" + tmpMessage.errorCode;
				this.login_again_event();

			}
		}
		/**重新登录事件 */
		private login_again_event() {
			this.log_again.addEventListener(egret.TouchEvent.TOUCH_TAP, this.login_again, this);
			this.log_again.visible = true;

		}
		/**重新登录 */
		private login_again() {
			this.initialize_login_main();
			this.log_again.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.login_again, this);
			this.oline_start();
		}
		/**帮助界面 */
		private help() {
			this.mainScene.visible = false;
			this.airBattleScene.visible = true;
			this.airBattleScene.addEventListener(egret.TouchEvent.TOUCH_TAP, this.airBattle, this);
		}
		private airBattle() {
			this.airBattleScene.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.airBattle, this);
			this.airBattleScene.visible = false;
			this.prepareScene.visible = true;
			this.prepareScene.addEventListener(egret.TouchEvent.TOUCH_TAP, this.prepare, this);
		}
		private prepare() {
			this.prepareScene.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.prepare, this);
			this.prepareScene.visible = false;
			this.mainScene.visible = true;
		}
		/**读取登录信息 */
		private async getLaunchInfo() {
			let msg = await platform.getLaunchOptionsSync();
			//为邀请状态
			if (msg != null && msg.query.key1 != null) {
				console.log("egret读取登录信息");
				StartUI.invited = true;
				messageManage.invitation = msg.query.key1;
				console.log(messageManage.invitation);
				let startInvite = new egret.Timer(2000, 1);
				startInvite.addEventListener(egret.TimerEvent.TIMER, this.oline_start, this);
				startInvite.start();
				this.invitation.visible = true;
				
			} 

		}
	}
}